#include "SandBox.hpp"
#include "../Geometry/Plane.hpp"
#include "../Model/MeshStructure.hpp"

using namespace Data;
using namespace App;
using namespace Model;
using namespace Geometry;
using namespace Program;
using namespace Renderer;

Plane plane;
MeshBuffer *planeBuffer = nullptr;
MeshRenderer *renderer = nullptr;

void SandBox::keyboardEvent(const KeyboardEvent &event) { __super::keyboardEvent(event); }

void SandBox::windowSizeChanged(int w, int h) { __super::windowSizeChanged(w, h); }

void SandBox::render() {
  __super::render();
  renderer->Draw();
}

void SandBox::mouseEvent(const MouseEvent &event) { __super::mouseEvent(event); }

void SandBox::setup() {
  __super::setup();
  auto planeMesh = plane.CreateMesh(true);
  planeBuffer = new MeshBuffer(planeMesh);
  renderer = new MeshRenderer(*planeBuffer);
}

void SandBox::renderEnd() { __super::renderEnd(); }

void SandBox::renderStart() { __super::renderStart(); }

void SandBox::windowFocused() { __super::windowFocused(); }

void SandBox::onGuiRender() {
  if (ImGui::Begin("Test")) {
    ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
                1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  }
  ImGui::End();
}

SandBox::~SandBox() {
  ProgramState::RemoveAll();
  std::cout << planeBuffer->GetMesh().use_count() << std::endl;

  delete planeBuffer;
  planeBuffer = nullptr;
  delete renderer;
  renderer = nullptr;
  MeshData::RemoveAll();
}
